package com.lvcoding.common;

import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author wuyanshen
 * @discription 描述
 * @date 2021-02-25 下午3:40
 */
@UtilityClass
public class SecurityUtil {


    /**
     * 获取当前登录用户
     *
     */
    public User getCurrentUser() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            final Object principal = authentication.getPrincipal();
            if (principal instanceof User) {
                return (User) principal;
            }
        }
        return null;
    }
}
